package com.hkw.servlet;

import com.hkw.model.User;
import com.hkw.service.IUserService;
import com.hkw.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 处理登入请求的servlet
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取到username和password
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(username,password);
        //实例化接口服务对象
        IUserService userService = new UserServiceImpl();
        //调用方法
        if(userService.login(user)==1){
            System.out.println("login成功!");
            response.sendRedirect("studentlist");
        }else{
            System.out.println("login失败!");
            request.setAttribute("err","该用户不存在,请输入正确的信息！");
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
